package cardwall.client.view;

import cardwall.client.ApplicationFacade;
import cardwall.client.model.TasksProxy;
import cardwall.shared.Task;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import org.puremvc.java.multicore.interfaces.INotification;

/**
 * @author $LastChangedBy: vogensen $
 * @version $Revision: 2 $
 */
public class CardDetailsDialogMediator extends Mediator {
    public CardDetailsDialogMediator(CardDetailsDialog cardDetailsDialog) {
        super("CardDetailsDialogMediator", cardDetailsDialog);
        getCardDetailsDialog().addUpdateButtonClickListener(new ClickListener() {
            public void onClick(Widget widget) {
                TasksProxy tasksProxy = (TasksProxy) ApplicationFacade.getInstance().retrieveProxy(TasksProxy.PROXYNAME);
                tasksProxy.updateTask(getCardDetailsDialog().getTask());
            }
        });
    }

    private CardDetailsDialog getCardDetailsDialog() {
        return (CardDetailsDialog) viewComponent;
    }

    public String[] listNotificationInterests() {
        return new String[] {ApplicationFacade.EDITTASK, ApplicationFacade.SELECTION_CHANGED};
    }

    public void handleNotification(INotification inotification) {
        if(inotification.getName().equals(ApplicationFacade.EDITTASK)) {
            getCardDetailsDialog().setTask((Task) inotification.getBody());
            getCardDetailsDialog().show();
        }
        else if(inotification.getName().equals(ApplicationFacade.SELECTION_CHANGED)) {
            getCardDetailsDialog().setTask((Task) inotification.getBody());
        }
    }
}